//
//  SingleProgrameViewController.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/27.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "CollectProgramaCell.h"
#import "AsyncGETConnection.h"
#import "SinglePullLayout.h"
#import "DailyCollectionCell.h"
#import "ArticleModel.h"
#import "DailyDetailViewController.h"

#import "CollectProgramaModel.h"
#import "CollectProgramaDatabase.h"

@interface CollectProgramaCell () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain) NSMutableArray * collectionArray;
@property (nonatomic,retain) UICollectionView * collection;
@property (nonatomic,retain) UILabel * zeroLabel;
@end

@implementation CollectProgramaCell
- (void)dealloc
{
    [_zeroLabel release];
    [_collection release];
    [_collectionArray release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.collectionArray = [NSMutableArray array];
        
        NSArray * databaseArray = [CollectProgramaDatabase selectAll];
        for (CollectProgramaModel * collectModel in databaseArray) {
            ArticleModel * model = [[ArticleModel alloc] init];
            model.id = collectModel.c_id;
            model.title = collectModel.c_title;
            
            // 组合simgs数组，里面只有一个元素，是一个字典，储存图片的尺寸和url
            NSMutableDictionary * simgsDic = [NSMutableDictionary dictionary];
            [simgsDic setValue:[NSNumber numberWithInteger:collectModel.c_height] forKey:@"height"];
            [simgsDic setValue:[NSNumber numberWithInteger:collectModel.c_width] forKey:@"width"];
            [simgsDic setValue:collectModel.c_url forKey:@"url"];
            NSArray * simgs = @[simgsDic];
            
            model.simgs = simgs;
            [self.collectionArray addObject:model];
            [model release];
        }
        
        [self createCollection];
        
        // 创建提示label，提示收藏是否为空
        self.zeroLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
        _zeroLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
        _zeroLabel.text = @"没有收藏";
        _zeroLabel.font = [UIFont boldSystemFontOfSize:25];
        _zeroLabel.textColor = [UIColor redColor];
        _zeroLabel.textAlignment = NSTextAlignmentCenter;
        [_collection addSubview:_zeroLabel];
        _zeroLabel.hidden = YES;
        [_zeroLabel release];
        
        // 如果收藏为空，提示label显示
        if(_collectionArray.count == 0){
            _zeroLabel.hidden = NO;
        }
    }
    return self;
}
-(void)createCollection{
    SinglePullLayout * layout = [[[SinglePullLayout alloc] init] autorelease];
    layout.dicArr = _collectionArray; // 要内存相等！！！！！
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.backgroundColor = [UIColor clearColor];
    [_collection registerClass:[DailyCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    [self.contentView addSubview:_collection];
    
    [_collection release];
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _collectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DailyCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ArticleModel * model = [_collectionArray objectAtIndex:indexPath.item];
    [cell setCellWithModel:model];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ArticleModel * model = [_collectionArray objectAtIndex:indexPath.item];
    [self enterDetailWithModel:model];
}
#pragma mark - DailyHeader协议
-(void)enterDetailWithModel:(ArticleModel *)model{
    [self.delegate enterDetailWithModel:model];
}
-(void)returnButtonClick{
    [self.delegate quitDetail];
}
#pragma mark - 视图将要出现刷新collection
-(void)viewWillAppearReloadData{
    [self.collectionArray removeAllObjects];
    
    NSArray * databaseArray = [CollectProgramaDatabase selectAll];
    for (CollectProgramaModel * collectModel in databaseArray) {
        ArticleModel * model = [[ArticleModel alloc] init];
        model.id = collectModel.c_id;
        model.title = collectModel.c_title;
        
        // 组合simgs数组，里面只有一个元素，是一个字典，储存图片的尺寸和url
        NSMutableDictionary * simgsDic = [NSMutableDictionary dictionary];
        [simgsDic setValue:[NSNumber numberWithInteger:collectModel.c_height] forKey:@"height"];
        [simgsDic setValue:[NSNumber numberWithInteger:collectModel.c_width] forKey:@"width"];
        [simgsDic setValue:collectModel.c_url forKey:@"url"];
        NSArray * simgs = @[simgsDic];
        
        model.simgs = simgs;
        [self.collectionArray addObject:model];
        [model release];
    }
    [_collection reloadData];
    
    // 根据收藏内容设置提示label是否显示
    if(_collectionArray.count == 0){
        _zeroLabel.hidden = NO;
    }else _zeroLabel.hidden = YES;
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
